package queue;

import java.util.Scanner;

public class CircleArrayQueueDemo {

    public static void main(String[] args) {
        //测试
        //创建一个队列
        CircleArrayQueue arrayQueue = new CircleArrayQueue(3);
        char key = ' ';//接受用户输入
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("s(show):展示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("a(add):添加队列数据");
            System.out.println("g(get):从队列取数据");
            System.out.println("h(head):查看队列头数据");
            key = sc.next().charAt(0);
            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'e':
                    loop = false;
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int add = sc.nextInt();
                    arrayQueue.addQueue(add);
                    break;
                case 'g':
                    try {
                        System.out.println(arrayQueue.getQueue());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 'h':
                    try {
                        System.out.println(arrayQueue.showFront());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                default:

                    System.out.println("输入错误");
            }

        }
    }
}

//使用数组模拟队列
class CircleArrayQueue{
    private int maxSize;//表示数组的最大容量
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr;//该数据用于存放数据，模拟队列

    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[this.maxSize];
//        front = 0;//指向队列头部，分析出front指向队列头前一个位置
//        rear = 0;//指向队列尾部，指向队列尾的数据
    }

    //判断队列是否满
    public boolean isFull(){
        return (rear + 1) % maxSize == front;
    }

    //判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int n){
        //判断队列是否满
        if (!isFull()){
            arr[rear] = n;
            rear = (rear + 1) % maxSize;
            return;
        }
        System.out.println("队列满不能加入数据");
    }

    //获取队列数据，出队列
    public int getQueue(){
        //判断队列是否空
        if (isEmpty()){
            //通过抛出异常
            throw new RuntimeException("队列空不能去数据");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    //显示队列所有数据
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空看不了");
            return;
        }
        for (int i = front;i<=front + sumArrCount();i++){
            System.out.printf("%d\t",arr[i % maxSize]);
        }
        System.out.println();
    }
    //求出当前队列有效数据个数
    public int sumArrCount(){
        return (rear + maxSize - front) % maxSize;
    }

    //显示队列头数据
    public int showFront(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }
}
